package Window;

import org.junit.Test;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Window
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-26  20:50
 * @Description: TODO
 * @Version: 1.0
 */
public class 最长回文子串5 {

    // 使用暴力的方法来实现
    public String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        int maxLen = 1;
        int begin = 0;
        char[] charArray = s.toCharArray();
        for (int i = 0; i < len - 1; i++) {
            for (int j = i + 1; j < len; j++) {
                if (j - i + 1 > maxLen && checkPalindrome(charArray, i, j)) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }

    private boolean checkPalindrome(char[] s, int left, int right) {
        if (left >= right) {
            return false;
        }
        while (left < right) {
            if (s[left] != s[right]) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    @Test
    public void test() {
        String s = longestPalindrome("cbbd");
        System.out.println(s);
    }

}
